<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> 
<head>
  
	<title>java check</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	
	 <script language="javascript" type="text/javascript"> 

	 function check(e){
		 var inputstring = document.getElementsByName("input")[0].value;
		 if(inputstring==""){
			 alert("请出入验证码!");
			 e.preventDefault(); //不执行与事件关联的默认动作
		 }
		 else{//执行ajax请求
			 var xmlhttp = null;
			 if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp=new XMLHttpRequest();
			 }
			 else{// code for IE6, IE5
			 	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			 }
			 
			 //发送请求-get方式
			 var url="check?inputstring="+inputstring+"&x=" + Math.random();
			 xmlhttp.open("GET",url,false);
			 xmlhttp.send();
			 
//			 //发送请求-post方式
//			 xmlhttp.open("POST","check",false);
//			 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//模拟HTTP POST方法发送一个表单
//			 xmlhttp.send("inputstring="+inputstring+"&x=" + Math.random());	 

		 	//显示结果
			alert(xmlhttp.responseText);
		 	
		 }
		 
	 }
	 
	 </script>
	
</head> 
 
<body> 

	<form action="" method="post">  
		<img src="image" onclick="this.src='image?'+Math.random()"/><br>
		<input type="text" name="input"/>  
		<input type="submit" value="验证" onclick="check(this)"> 
	</form>  

</body> 
</html>